import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { CarouselService } from './carousel.service';
import { Carousel } from './entities/carousel.entity';
import { CreateCarouselDto, UpdateCarouselDto, CarouselResponseDto } from './dto/carousel.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles } from '../auth/decorators/roles.decorator';
import { UserRole } from '../auth/enums/user.enum';
import { PaginationDto, PaginatedResponseDto } from './dto/pagination.dto';

@ApiTags('走马灯')
@Controller('carousels')
export class CarouselController {
  constructor(private readonly carouselService: CarouselService) {}

  @Post()
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @ApiBearerAuth()
  @ApiOperation({ summary: '创建走马灯', description: '创建新的走马灯项目' })
  @ApiResponse({ status: 201, description: '创建成功', type: CarouselResponseDto })
  create(@Body() createCarouselDto: CreateCarouselDto): Promise<Carousel> {
    return this.carouselService.create(createCarouselDto);
  }

  @Get()
  @ApiOperation({ summary: '获取所有走马灯', description: '获取所有走马灯项目（不分页）' })
  @ApiResponse({ status: 200, description: '获取成功', type: [CarouselResponseDto] })
  findAll(): Promise<Carousel[]> {
    return this.carouselService.findAll();
  }

  @Get('paginated')
  @ApiOperation({ summary: '分页获取走马灯', description: '分页获取所有走马灯项目' })
  @ApiResponse({ status: 200, description: '获取成功', type: PaginatedResponseDto })
  findAllPaginated(@Query() paginationDto: PaginationDto): Promise<PaginatedResponseDto<Carousel>> {
    return this.carouselService.findAllPaginated(paginationDto);
  }

  @Get('active')
  @ApiOperation({ summary: '获取激活的走马灯', description: '获取所有激活状态的走马灯项目（不分页）' })
  @ApiResponse({ status: 200, description: '获取成功', type: [CarouselResponseDto] })
  findActive(): Promise<Carousel[]> {
    return this.carouselService.findActive();
  }

  @Get('active/paginated')
  @ApiOperation({ summary: '分页获取激活的走马灯', description: '分页获取所有激活状态的走马灯项目' })
  @ApiResponse({ status: 200, description: '获取成功', type: PaginatedResponseDto })
  findActivePaginated(@Query() paginationDto: PaginationDto): Promise<PaginatedResponseDto<Carousel>> {
    return this.carouselService.findActivePaginated(paginationDto);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取单个走马灯', description: '根据ID获取走马灯项目' })
  @ApiResponse({ status: 200, description: '获取成功', type: CarouselResponseDto })
  @ApiResponse({ status: 404, description: '走马灯不存在' })
  findOne(@Param('id') id: string): Promise<Carousel> {
    return this.carouselService.findOne(+id);
  }

  @Patch(':id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @ApiBearerAuth()
  @ApiOperation({ summary: '更新走马灯', description: '根据ID更新走马灯项目' })
  @ApiResponse({ status: 200, description: '更新成功', type: CarouselResponseDto })
  @ApiResponse({ status: 404, description: '走马灯不存在' })
  update(
    @Param('id') id: string,
    @Body() updateCarouselDto: UpdateCarouselDto,
  ): Promise<Carousel> {
    return this.carouselService.update(+id, updateCarouselDto);
  }

  @Delete(':id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(UserRole.ADMIN)
  @ApiBearerAuth()
  @ApiOperation({ summary: '删除走马灯', description: '根据ID删除走马灯项目' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '走马灯不存在' })
  remove(@Param('id') id: string): Promise<void> {
    return this.carouselService.remove(+id);
  }
} 